package com.ruoyi.community.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.ruoyi.common.enums.FeeAndRepairStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CommunityFeeMapper;
import com.ruoyi.community.domain.CommunityFee;
import com.ruoyi.community.service.ICommunityFeeService;
import sun.util.calendar.CalendarUtils;

/**
 * 物业费用管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-18
 */
@Service
public class CommunityFeeServiceImpl implements ICommunityFeeService
{
    @Autowired
    private CommunityFeeMapper communityFeeMapper;

    /**
     * 查询物业费用管理
     *
     * @param id 物业费用管理主键
     * @return 物业费用管理
     */
    @Override
    public CommunityFee selectCommunityFeeById(Long id)
    {
        return communityFeeMapper.selectCommunityFeeById(id);
    }

    /**
     * 查询物业费用管理列表
     *
     * @param communityFee 物业费用管理
     * @return 物业费用管理
     */
    @Override
    public List<CommunityFee> selectCommunityFeeList(CommunityFee communityFee)
    {
        return communityFeeMapper.selectCommunityFeeList(communityFee);
    }

    @Override
    public Map<String,BigDecimal> selectCommunityFeeTotalAmount() {
        Map<String, BigDecimal> resultMap = new HashMap<>();
        CommunityFee communityFeeQuery = new CommunityFee();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //格式化时间
        String format = simpleDateFormat.format(new Date());
        //获取base实体类对象来传递投诉时间
        communityFeeQuery.getParams().put("totalTime",format);
        communityFeeQuery.setStatus(FeeAndRepairStatus.OK.getCode());
        BigDecimal totalResult = new BigDecimal("0");
        totalResult = getTotalAmount(communityFeeQuery, totalResult,"today");
        resultMap.put("today",totalResult);
        //日期获得昨天的现在，并且格式化
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,-1);
        Date time = calendar.getTime();
        format = simpleDateFormat.format(time);
        communityFeeQuery.getParams().put("totalTime",format);
        totalResult = getTotalAmount(communityFeeQuery, totalResult,"yesterday");
        resultMap.put("yesterday",totalResult);
        totalResult = getTotalAmount(communityFeeQuery, totalResult,"mouth");
        resultMap.put("mouth",totalResult);
        totalResult = getTotalAmount(communityFeeQuery, totalResult,"year");
        resultMap.put("year",totalResult);
        return resultMap;
    }

    /**
     * 计算总金额
     * @param communityFeeQuery
     * @param totalResult
     * @return
     */
    private BigDecimal getTotalAmount(CommunityFee communityFeeQuery, BigDecimal totalResult,String timeType) {
        totalResult = new BigDecimal("0");
        communityFeeQuery.getParams().put("timeType",timeType);
        List<CommunityFee> list =communityFeeMapper.selectCommunityFeeList(communityFeeQuery);
        for (CommunityFee fee : list) {
            BigDecimal amount = new BigDecimal(fee.getAmount());
            totalResult = totalResult.add(amount);
        }
        return totalResult;
    }

    /**
     * 新增物业费用管理
     *
     * @param communityFee 物业费用管理
     * @return 结果
     */
    @Override
    public int insertCommunityFee(CommunityFee communityFee)
    {
        communityFee.setFeeId(UUID.randomUUID().toString());
        return communityFeeMapper.insertCommunityFee(communityFee);
    }

    /**
     * 修改物业费用管理
     *
     * @param communityFee 物业费用管理
     * @return 结果
     */
    @Override
    public int updateCommunityFee(CommunityFee communityFee)
    {
        return communityFeeMapper.updateCommunityFee(communityFee);
    }

    /**
     * 批量删除物业费用管理
     *
     * @param ids 需要删除的物业费用管理主键
     * @return 结果
     */
    @Override
    public int deleteCommunityFeeByIds(Long[] ids)
    {
        return communityFeeMapper.deleteCommunityFeeByIds(ids);
    }

    /**
     * 删除物业费用管理信息
     *
     * @param id 物业费用管理主键
     * @return 结果
     */
    @Override
    public int deleteCommunityFeeById(Long id)
    {
        return communityFeeMapper.deleteCommunityFeeById(id);
    }
}
